<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>v-model基本使用</title>
    <script src="../lib/vue_v2.6.14.js"></script>
  </head>
  <body>
    <div id="app">
      <!-- v-model 动态双向绑定 -->
      <input type="text" v-model="inputVal" />
      <h3>input值：{{ inputVal }}</h3>
    </div>

    <script>
      const vm = new Vue({
        el: '#app',
        data: {
          inputVal: '',
          timer: 0
        },
        created() {
          this.getNowTime()
        },
        methods: {
          // 获取当前时间
          getNowTime() {
            // 清除定时器
            if (this.timer !== 0) clearInterval(this.timer)

            let dt = new Date()
            let year = dt.getFullYear()
            let month = dt.getMonth() + 1
            month = month < 10 ? '0' + month : month
            let day = dt.getDate()
            day = day < 10 ? '0' + day : day
            let hour = dt.getHours()
            hour = hour < 10 ? '0' + hour : hour
            let min = dt.getMinutes()
            min = min < 10 ? '0' + min : min
            let sec = dt.getSeconds()
            sec = sec < 10 ? '0' + sec : sec
            this.inputVal = `${year}-${month}-${day} ${hour}:${min}:${sec}`

            this.startTime()
          },
          startTime() {
            this.timer = setInterval(() => {
              this.getNowTime()
            }, 1000)
          }
        }
      })
    </script>
  </body>
</html>
